﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace GUIL
{
    public partial class BookingConfirmationGUI : Form, RemoteService.IRemoteServiceCallback
    {
        static RemoteService.IRemoteService _remote;

        RemoteService.Booking _booking;

        public BookingConfirmationGUI(RemoteService.Booking booking)
        {
            // Hosting client interface ... (more info later)
            // Connection hasn't been opened yet!
            _remote = new RemoteService.RemoteServiceClient(new InstanceContext(this), "WSDualHttpBinding_IRemoteService");

            _booking = booking;

            InitializeComponent();
        }

        private void BookingConfirmationGUI_Load(object sender, EventArgs e)
        {
            // calculate total price
            int totalPrice = (_booking.ScreenCast.Movie.Price + _booking.ScreenCast.Price) * _booking.PartBookings.Count; // because the price counts for all the seats/part bookings
            foreach (var item in _booking.PartBookings)
            {
                switch (item.AgeGroup)
                {
                    case "1": // do not add price
                        break;
                    case "2": // both 2 and 3 get 10kr discount
                    case "3":
                        totalPrice -= 10;
                        break;
                    default: // do nothing
                        break;
                }

                totalPrice += item.ScreenCast_Seat.Seat.Price; // add price for the seat
            }

            // set static info
            labelMovie.Text = _booking.ScreenCast.Movie.Title;
            labelHall.Text = _booking.ScreenCast.MovieHall.Number + "";
            labelDate.Text = _booking.ScreenCast.Airdate.ToShortDateString();
            labelTime.Text = _booking.ScreenCast.Airdate.ToShortTimeString();
            labelTotalPrice.Text = totalPrice + " Kr.";
            dataGridViewSeats.DataSource = _booking.PartBookings.Select(pb => pb.ScreenCast_Seat.Seat).OrderBy(s => s.Row).ThenBy(s => s.Number).ToList(); // get all seats, ordered by row, number ASC

            // need to set user before being able to complete booking
            buttonChangeCustomer.Enabled = false; // being enabled when masked text box is comletely filled out
            buttonBook.Enabled = false;
        }

        private void maskedTextBoxCustomerChange_KeyUp(object sender, KeyEventArgs e)
        {
            // if input done make change button available
            buttonChangeCustomer.Enabled = maskedTextBoxCustomerChange.MaskCompleted;

            if (buttonChangeCustomer.Enabled && e.KeyCode==Keys.Enter) // if button is enabled and key pressed is enter, do the button_click method
            {
                buttonChangeCustomer_Click(null, null);
            }
        }

        private void maskedTextBoxCustomerChange_Validated(object sender, EventArgs e)
        {
            // if validation succeeds make change button available
            buttonChangeCustomer.Enabled = maskedTextBoxCustomerChange.MaskCompleted;
        }

        private void buttonChangeCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                // set customer info on booking and in gui
                _booking.User = _remote.GetCustomer(maskedTextBoxCustomerChange.Text.Replace(" ",null));
                labelCustomer.Text = _booking.User.Name + " - " + _booking.User.PhoneNo;

                // empty masked text box and disable change button
                maskedTextBoxCustomerChange.Text = "";
                buttonChangeCustomer.Enabled = false;

                // enable book button
                buttonBook.Enabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show("No customer with that phone number, try again");
            }
        }

        private void buttonBook_Click(object sender, EventArgs e)
        {
            try
            {
                _remote.SaveBooking(_booking);
                // close with ok
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Booking Failed!");
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            // close with cancel
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        public void OnAction(List<RemoteService.ScreenCast_Seat> screenSeats)
        {
            throw new NotImplementedException(); // EAT IT!
        }

        private void BookingConfirmationGUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            // free seats if window is closed irregularly
            if (this.DialogResult == DialogResult.Cancel || this.DialogResult == DialogResult.Abort || this.DialogResult == DialogResult.Ignore)
            {
                // Get screenSeats from partbookings
                List<RemoteService.ScreenCast_Seat> selectedSeats = _booking.PartBookings.Select(pb => pb.ScreenCast_Seat).ToList();

                // change availability back to free
                selectedSeats.ForEach(s => s.SeatAvailable = true);

                // Change seats status in db
                _remote.ChangeSeatsAvailability(selectedSeats);
            }
        }
    }
}
